home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 8981 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.6 KB

  1. Path: anvil.ugrad.cs.ubc.ca!not-for-mail
  2. From: c2a192@ugrad.cs.ubc.ca (Kazimir Kylheku)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Byte Orders and Char Sets?
  5. Date: 6 Mar 1996 15:11:50 -0800
  6. Organization: Computer Science, University of B.C., Vancouver, B.C., Canada
  7. Message-ID: <4hl63mINNdjb@anvil.ugrad.cs.ubc.ca>
  8. References: <4hkqm1$5dm@madeline.INS.CWRU.Edu>
  9. NNTP-Posting-Host: anvil.ugrad.cs.ubc.ca
  10.  
  11. In article <4hkqm1$5dm@madeline.INS.CWRU.Edu>,
  12. Michael A. Balfour <mab22@po.CWRU.Edu> wrote:
  13.  >
  14.  >Hi!
  15.  >
  16.  >In the hopes of writing some portable code, I'm trying to 
  17.  >determine the byte order and character set that my program is
  18.  >compiled in.  Does anybody have a faster or cleaner way than
  19.  >this:
  20.  >
  21.  >#define NativeCharSet() ((' '==0x40) ? EBCDIC_CHAR_SET : ASCII_CHAR_SET)
  22.  >
  23.  >static unsigned short int nativeIntTest=0x0001;
  24.  >static char *nativeIntTest2=(char *)&nativeIntTest;
  25.  >#define NativeByteOrder() ((nativeIntTest2[0]==0x00) ? LITTLE_ENDIAN : BIG_ENDIAN)
  26.  >
  27.  >(where the char sets and byte orders are also defined)
  28.  >
  29.  >Thank you very much,
  30.  
  31. Why don't you pre-compute these values into global constants? NativeByteOrder()
  32. might not optimize away.
  33.  
  34. You can write a separate program which performs these tests, and then generates
  35. a header file which you can include in another program. When installing your
  36. software, you compile the generator first, then run it to create the include
  37. file. This could be part of an automated ``make'' process or script.
  38.  
  39. The include file will have simple manifest constants that tell you what
  40. character set you are using, etc. The advantage is that these can be edited
  41. manually, should there be a problem.
  42. -- 
  43.  
  44.